<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="addRole">+ 新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text">分配权限</el-button>
                  <!-- {{ row }} -->
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div>
            <el-pagination
              :current-page="page.page"
              :page-size="page.pagesize"
              :page-sizes="[1, 10, 100, 200]"
              layout="total ,sizes, prev , pager, next , jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <component :is="'Info'" />
          <!-- <Info /> -->
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- <DialogItem
      ref="dialogitem"
      :is-show.sync="show"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    /> -->
    <DialogItem
      ref="dialogitem"
      v-model="show"
      :list="list"
      @currentChange="currentChange"
      @getData="getData"
    />
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import DialogItem from './component/dialogitem.vue'
// import Info from './component/info.vue'
export default {
  components: {
    DialogItem,
    // 模块按需加载 异步组件
    Info: () => import('./component/info.vue')
  },
  data() {
    return {
      show: false,
      page: {
        page: 1,
        pagesize: 1,
        total: 100
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取角色列表信息
    async getData() {
      const res = await sysRole(this.page)
      // 角色列表数据
      this.list = res.data.rows
      this.page.total = res.data.total
      // console.log(res)
    },
    // 页码改变
    currentChange(page) {
      this.page.page = page
      this.getData()
    },
    // 页容量改变
    sizeChange(size) {
      console.log(size)
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 新增角色
    addRole() {
      this.show = true
      this.$refs.dialogitem.mode = 'add'
    },
    // 删除角色
    del(id) {
      this.$confirm('你确定删除该角色吗？', '提示').then(async() => {
        await sysRoleDel(id)
        this.$message.success('删除角色成功')
        this.currentChange(1)
      })
    },
    // 编辑
    edit(row) {
      // 1 掉起弹框 2 回显数据
      this.show = true
      this.$refs.dialogitem.form = { ...row }
      this.$refs.dialogitem.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
}
</style>
